package com.industrial.app.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.industrial.app.domain.BizIndustrialValueAddedByCity;
import com.industrial.app.domain.formal.BizIndustrialValueAddedByCityFormal;
import com.industrial.app.domain.request.BizIndustrialValueAddedByCityRequest;
import com.industrial.app.mapper.BizIndustrialValueAddedByCityFormalMapper;
import com.industrial.app.service.IBizIndustrialValueAddedByCityFormalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分市工业增加值正式Service业务层处理
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@Service
public class BizIndustrialValueAddedByCityFormalServiceImpl implements IBizIndustrialValueAddedByCityFormalService
{
    @Autowired
    private BizIndustrialValueAddedByCityFormalMapper bizIndustrialValueAddedByCityFormalMapper;

    /**
     * 查询分市工业增加值正式
     *
     * @param baseId 分市工业增加值正式主键
     * @return 分市工业增加值正式
     */
    @Override
    public BizIndustrialValueAddedByCityFormal selectBizIndustrialValueAddedByCityFormalByBaseId(String baseId)
    {
        return bizIndustrialValueAddedByCityFormalMapper.selectBizIndustrialValueAddedByCityFormalByBaseId(baseId);
    }

    /**
     * 查询分市工业增加值正式列表
     *
     * @param bizIndustrialValueAddedByCityFormal 分市工业增加值正式
     * @return 分市工业增加值正式
     */
    @Override
    public List<BizIndustrialValueAddedByCityFormal> selectBizIndustrialValueAddedByCityFormalList(BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        return bizIndustrialValueAddedByCityFormalMapper.selectBizIndustrialValueAddedByCityFormalList(bizIndustrialValueAddedByCityFormal);
    }

    /**
     * 新增分市工业增加值正式
     *
     * @param bizIndustrialValueAddedByCityFormal 分市工业增加值正式
     * @return 结果
     */
    @Override
    public int insertBizIndustrialValueAddedByCityFormal(BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        return bizIndustrialValueAddedByCityFormalMapper.insertBizIndustrialValueAddedByCityFormal(bizIndustrialValueAddedByCityFormal);
    }

    /**
     * 修改分市工业增加值正式
     *
     * @param bizIndustrialValueAddedByCityFormal 分市工业增加值正式
     * @return 结果
     */
    @Override
    public int updateBizIndustrialValueAddedByCityFormal(BizIndustrialValueAddedByCityFormal bizIndustrialValueAddedByCityFormal)
    {
        return bizIndustrialValueAddedByCityFormalMapper.updateBizIndustrialValueAddedByCityFormal(bizIndustrialValueAddedByCityFormal);
    }

    /**
     * 批量删除分市工业增加值正式
     *
     * @param baseIds 需要删除的分市工业增加值正式主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustrialValueAddedByCityFormalByBaseIds(String[] baseIds)
    {
        return bizIndustrialValueAddedByCityFormalMapper.deleteBizIndustrialValueAddedByCityFormalByBaseIds(baseIds);
    }

    /**
     * 删除分市工业增加值正式信息
     *
     * @param baseId 分市工业增加值正式主键
     * @return 结果
     */
    @Override
    public int deleteBizIndustrialValueAddedByCityFormalByBaseId(String baseId)
    {
        return bizIndustrialValueAddedByCityFormalMapper.deleteBizIndustrialValueAddedByCityFormalByBaseId(baseId);
    }

    /**
     * 查询分市工业增加值列表
     *
     * @param request
     * @return
     */
    @Override
    public List<BizIndustrialValueAddedByCity> getIndustrialValueAddedByCityList(BizIndustrialValueAddedByCityRequest request) {
        String specifiedStr = request.getDates();
        DateTime specifiedDate = DateUtil.parse(specifiedStr, "yyyy年MM月");
        request.setDates(DateUtil.format(specifiedDate,"yyyy-MM-dd"));
        List<BizIndustrialValueAddedByCity> result = bizIndustrialValueAddedByCityFormalMapper.getIndustrialValueAddedByCityList(request);
        return result;
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizIndustrialValueAddedByCityFormalMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }
}
